<?php
jimport('joomla.application.component.model');
class svModelCanbo extends JModel
{
	//------------ tim kiem----------------------------------
	function getAllCanbo($page)
	{
		if($page==0) $page=1;
		// so ban ghi hien thi tren mot trang la 10
		$numberspage=10;
		$start=($page-1)*$numberspage;
		$end=$numberspage;
		$query='SELECT  cb.*, qlc.*, cv.cv_ten as chucvu,pb.pb_ten as phongban,cd.cd_ten as chucdanh
			    FROM    qlcb_canbo as cb,qlcb_quanlychung as qlc,qlcb_phongban as pb,qlcb_chucvu as cv, qlcb_chucdanh as cd
			    WHERE   cb.cb_id = qlc.qlc_idcanbo  
				AND		qlc.qlc_idphongban=pb.pb_id 
				AND		qlc.qlc_idchucvu=cv.cv_id 
				AND		qlc.qlc_idchucdanh=cd.cd_id 
				ORDER BY cb.cb_id DESC LIMIT '.$start.','.$end;
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getCanboByID($idcanbo)
	{
		$query='SELECT  cb.*, qlc.*, cv.cv_ten as chucvu,pb.pb_ten as phongban,cd.cd_ten as chucdanh
				FROM    qlcb_canbo as cb,qlcb_quanlychung as qlc,qlcb_phongban as pb,qlcb_chucvu as cv, qlcb_chucdanh as cd
				WHERE   cb.cb_id = qlc.qlc_idcanbo
				AND		qlc.qlc_idphongban=pb.pb_id
				AND		qlc.qlc_idchucvu=cv.cv_id
				AND		qlc.qlc_idchucdanh=cd.cd_id  
				AND 	cb.cb_id='.$idcanbo;
		$result=$this->_loadObject($query);
		return $result;
	}
	function getCanboByIDs($idcanbo)
	{
		$query='SELECT  cb.cb_id,cb.cb_ten,cb.cb_namsinh,cb.cb_hinhanh,cb.cb_diachi,
		cv.cv_ten as chucvu,pb.pb_ten as phongban,cd.cd_ten as chucdanh
		FROM    qlcb_canbo as cb,qlcb_quanlychung as qlc,qlcb_phongban as pb,qlcb_chucvu as cv, qlcb_chucdanh as cd
		WHERE   cb.cb_id = qlc.qlc_idcanbo
		AND		qlc.qlc_idphongban=pb.pb_id
		AND		qlc.qlc_idchucvu=cv.cv_id
		AND		qlc.qlc_idchucdanh=cd.cd_id
		AND 	cb.cb_id='.$idcanbo;
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getAllPhongban()
	{
		$query='SELECT pb_id,pb_ten
				FROM qlcb_phongban
				WHERE 1';
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getAllChucvu()
	{
		$query='SELECT cv_id,cv_ten
		FROM qlcb_chucvu
		WHERE 1';
		$result=$this->_loadObjectList($query);
		return $result;
	}
	function getAllChucdanh()
	{
		$query='SELECT cd_id,cd_ten
		FROM qlcb_chucdanh
		WHERE 1';
		$result=$this->_loadObjectList($query);
		return $result;
	}
	//----------------------------them ,sua ,xoa -------------------------------
	function addNewCanbo($ten,$namsinh,$quequan,$cmnd,$gioitinh,$diachi,$dienthoai,$tongiao,$honnhan,$hinhanh,$mieuta,$dantoc)
	{
		$query="INSERT INTO qlcb_canbo(cb_ten,cb_namsinh,cb_quequan,cb_cmnd,cb_gioitinh,cb_diachi,cb_sodienthoai,cb_tongiao,cb_honnhan,cb_hinhanh,cb_mieuta,cb_dantoc)
		VALUES('".$ten."','".$namsinh."','".$quequan."','".$cmnd."','".$gioitinh."','".$diachi."','".$dienthoai."',
		'".$tongiao."','".$honnhan."','".$hinhanh."','".$mieuta."','".$dantoc."')";
		$message='Thêm mới ';
		$result=$this->_Execute($query, $message);
		return $result.' <b style="font-size:18px;color:blue;">'.$ten.'</b>';
	}
	function addNewQuanlychung($idphongban,$idchucvu,$idchucdanh,$idcanbo)
	{
		$query="INSERT INTO qlcb_quanlychung(qlc_idphongban,qlc_idchucvu,qlc_idchucdanh,qlc_idcanbo)
		 VALUES('".$idphongban."','".$idchucvu."','".$idchucdanh."','".$idcanbo."')";
		$message='Thêm mới ';
		$result=$this->_Execute($query, $message);		
		return $result;
	}
	function getMaxIDCanbo()
	{
		$query='SELECT MAX(cb_id) as max
		FROM qlcb_canbo
		WHERE 1';
		$result=$this->_loadObject($query);
		return $result->max;
	}
	// sua
	function editCanbo($idcanbo,$ten,$namsinh,$quequan,$cmnd,$gioitinh,$diachi,$dienthoai,$tongiao,$honnhan,$hinhanh,$mieuta,$dantoc)
	{
		$query="UPDATE qlcb_canbo 
				SET cb_mieuta='".$mieuta."', cb_ten='".$ten."', cb_ten='".$ten."', cb_namsinh='".$namsinh."',
					cb_quequan='".$quequan."', cb_cmnd='".$cmnd."', cb_gioitinh='".$gioitinh."', cb_diachi='".$diachi."',
					cb_sodienthoai='".$dienthoai."', cb_tongiao='".$tongiao."', cb_honnhan='".$honnhan."', cb_hinhanh='".$hinhanh."', cb_dantoc='".$dantoc."'
	   		    WHERE cb_id=".$idcanbo;
		$message='Chỉnh sửa ';
		$result=$this->_Execute($query, $message);
		return $result.' <b style="font-size:18px;color:blue;">'.$ten.'</b>';
	}
	function editQuanlychung($idphongban,$idchucvu,$idchucdanh,$idqlc)
	{
		$query="UPDATE qlcb_quanlychung
		SET qlc_idphongban='".$idphongban."', qlc_idchucvu='".$idchucvu."', qlc_idchucdanh='".$idchucdanh."'
		WHERE qlc_id=".$idqlc;
		$message='Chỉnh sửa 1 item ';
		$result=$this->_Execute($query, $message);
		return $result;
	}
	function delChucdanh($idchucvu)
	{
		$query="DELETE FROM qlcb_chucdanh WHERE cd_id=".$idchucvu;
		$message='Xóa 1 item ';
		$result=$this->_Execute($query, $message);
		return $result;	
	}
	// cac ham phuc vu truy van CSDL
	function _loadObject($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObject();
		return $result;
	}
	function _loadObjectList($query)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		$result=$db->loadObjectList();
		return $result;
	}
	function _Execute($query,$message)
	{
		$db= JFactory::getDBO();
		$db->setQuery($query);
		try {
			$db->query();
			return $message." thành công ";
		}
		catch (Exception $e){
			return $message." Không thành công! Vui lòng thử lại.";
		}
	}
}
?>